﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace Framework.Mvc.Attributes
{
    [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
    public class DataTablesVisibleAttribute : Attribute
    {
        public DataTablesVisibleAttribute()
            : this(true)
        { }
        public DataTablesVisibleAttribute(bool visible)
        {
            this.Visible = visible;
        }

        public bool Visible { get; set; }
    }

    [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
    public class DataTableOrderByIndexAttribute : Attribute
    {
        public DataTableOrderByIndexAttribute() { }
        public DataTableOrderByIndexAttribute(int index)
        {
            Index = index;
        }
        public int Index { get; set; }
    }

    /// <summary>
    /// Based custom authorization data to Check user whether is authorized
    /// </summary>
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)]
    public class AuthorizeFilterAttribute : ActionFilterAttribute
    {
        FilterContextInfo fcinfo;

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            fcinfo = new FilterContextInfo(filterContext);

            bool isLogin = HttpContext.Current.User.Identity.IsAuthenticated;
            //身份验证和授权代码
            if (isLogin)
            {
                //filterContext.Result = new HttpUnauthorizedResult();
                //filterContext.Result = new RedirectResult("http://www.baidu.com");
                //filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new { Controller = fcinfo.controllerName, action = fcinfo.actionName }));
            }
            else
            {
                filterContext.Result = new ContentResult
                {
                    Content = "您没有当前操作的权限！",
                    ContentEncoding = Encoding.UTF8
                };
            }
        }

    }

    /// <summary>
    /// Info of context filter
    /// </summary>
    public class FilterContextInfo
    {
        public FilterContextInfo(ActionExecutingContext filterContext)
        {
            #region 获取链接中的字符
            // 获取域名  
            domainName = filterContext.HttpContext.Request.Url.Authority;
            //获取模块名称    
            module = filterContext.HttpContext.Request.Url.Segments[1].Replace('/', ' ').Trim();
            //获取 controllerName 名称     
            controllerName = filterContext.RouteData.Values["controller"].ToString();
            //获取ACTION 名称        
            actionName = filterContext.RouteData.Values["action"].ToString();
            #endregion
        }

        public string domainName { get; set; }
        public string module { get; set; }
        public string controllerName { get; set; }
        public string actionName { get; set; }
    }
}
